#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void PrintArr(int* arr, int n)
{
	for (int i = 0; i < n; i++)
		printf("%d \n", arr[i]);
}
void CountSort(int* a, int n)
{
	int max = a[0], min = a[0];
	for (int i = 0; i < n; i++)
	{
		if (a[i] > max)
			max = a[i];
		if (a[i] < min)
			min = a[i];
	}
	int range = max - min + 1;
	int* count = (int*)calloc(range, sizeof(int));
	for (int i = 0; i < n; i++)
		count[a[i] - min]++;
	int j = 0;
	for (int i = 0; i < range; i++)
	{
		while (count[i]--)
		{
			a[j++] = i + min;
		}
	}
	free(count);
	count = NULL;
}
void TestCountSort()
{
	srand(time(0));
	int n = 100;
	int* a = (int*)malloc(sizeof(int) * n);
	for (int i = 0; i < n; i++)
		a[i] = rand() % 1000;
	CountSort(a, n);
	PrintArr(a, n);
	for (int i = 0; i < n - 1; i++)
	{
		if (a[i] > a[i + 1])
		{
			printf("error");
			break;
		}
	}
	free(a);
	a = NULL;
}
int main()
{
	TestCountSort();
	return 0;
}